#ifndef QTOGRE_QOGREWIDGET_H
#define QTOGRE_QOGREWIDGET_H

#include "OGRE/Ogre.h"
#include "IOgreEventHandler.h"
#include "QCameraMan.h"
#include "Bullet/physics.h"
#include "MyObject.h"
#include "Game.h"

#include <Overlay\OgreOverlay.h>
#include <QtOpenGL/QGLWidget>
#include <Terrain/OgreTerrain.h>
#include <Terrain/OgreTerrainGroup.h>


#include <SdkTrays.h>

#  include <OISEvents.h>
//*********************************************************************************************************************
/** Qt widget that wraps an embedded ogre render window
 *
 * This classes only responsibility is to initialise the ogre render window. The Ogre::Root object
 * to be used must be supplied by the user and once initialised the intended usage is to extract the
 * ogre render window and manipulate it directly to create the desired scene. The underlying widget
 * takes care of rendering and resizing when necessary.
 */
class QOgreWidget : public QGLWidget, public Ogre::FrameListener {
    Q_OBJECT

public: // interface
    /** Constructor
     * @param parent is required because ogre needs to set it as it's parent during initialisation
     * @throws if parent is NULL
     * @post Ogre3D renderwindow has been created
     */
    QOgreWidget( IOgreEventHandler *ogreEventHandler, QWidget *parent);


	
    void setupRenderSystem();
    void setupResources();
    void createScene();
	void chooseSceneManager(void);
	void renderOneFrame();

    Ogre::RenderWindow *getEmbeddedOgreWindow();
	OgreBites::QCameraMan* getEmbeddedQCameraMan();
	void cameraManReady(bool);
	void setCameraMan(OgreBites::QCameraMan* camMan);
	void addEmbeddedFrameListener();
	
	Physics* getPhysicsModule();
	OgreBites::QCameraMan* getCameraMan();

	void airplaneMove(float dx, float dy, float dz);
	void airplaneInpulse(float x, float y, float z);
	void applyForceToAirplane(float fx, float fy, float fz);
	void applyTorqueToAirplane(float fx, float fy, float fz);
	void setFingerFactor(int num);


	void setAirplanePose(float azi, float pitch, float roll);
	
    Ogre::TerrainGlobalOptions* mTerrainGlobals;
    Ogre::TerrainGroup* mTerrainGroup;
    bool mTerrainsImported;
    //OgreBites::Label* mInfoLabel;

	void getTerrainImage(bool flipX, bool flipY, Ogre::Image& img);
    void defineTerrain(long x, long y);
    void initBlendMaps(Ogre::Terrain* terrain);
    void configureTerrainDefaults(Ogre::Light* light);

	
	void initObjects();

	

	
protected: // interface
    /** @name Inherited from QWidget
     */
    //@{
    virtual void paintEvent(QPaintEvent *pEvent);
    virtual void resizeEvent(QResizeEvent *rEvent);
    virtual void update();
    virtual void mousePressEvent(QMouseEvent *event);
    virtual void mouseMoveEvent(QMouseEvent *event);
	virtual void keyPressEvent(QKeyEvent *event);
	virtual void keyReleaseEvent(QKeyEvent *event);
	
	
    virtual void createFrameListener(void);
	virtual bool frameRenderingQueued(const Ogre::FrameEvent& evt);
    //@}
    

private:
	

    Ogre::Root *mOgreRoot;
    Ogre::RenderWindow *mOgreRenderWindow;
    Ogre::Viewport *mOgreViewport;
    Ogre::RenderSystem *mRenderSystem;
    Ogre::SceneManager *mSceneManager;
    Ogre::OverlaySystem*        mOverlaySystem;

	
    Ogre::Camera *mCamera;
	OgreBites::QCameraMan* mCameraMan;
  
    IOgreEventHandler *mOgreEventHandler;
	Physics *mPhysicsModule;

	
	//game object
	MyObject *airplane;
	Ogre::Vector3 mForce;

	int fingerFactor;

	bool isCameraManReady;


private:

	int gameState;
	
	

}; // class

#endif // QTOGRE_QOGREWIDGET_H
